﻿using Masuit.Tools;
namespace Tgather
{
    public partial class SetForm : Form
    {
        private Config config = new Config();

        public SetForm()
        {
            InitializeComponent();

        }

        private void SetForm_Load(object sender, EventArgs e)
        {
            dPortTextBox.Text = config.DEPort;
            dBaudRateComboBox.Text = config.DEBaudRate;
            dAddressTextBox.Text = config.DEAddress;
            dbHostTextBox.Text = config.DBHost;
            dbPortTextBox.Text = config.DBPort;
            dbDatebaseTextBox.Text = config.DBDatabase;
            dbUserTextBox.Text = config.DBUser;
            dbPwdTextBox.Text = config.DBPwd;
            dbPTableTextBox.Text = config.DBPTable;
            dbPointTableTextBox.Text = config.DBPointTable;
            apiAddressTextBox.Text = config.APIAddress;
        }
       
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void confirmButton_Click(object sender, EventArgs e)
        {
            if (dPortTextBox.Text.IsNullOrEmpty())
            {
               Alert.Error("请输入设备端口");
               return;
            }
           config.DEPort = dPortTextBox.Text;

            if (dBaudRateComboBox.Text.IsNullOrEmpty())
            {
                Alert.Error("请选择波特率");
                return;
            }
            config.DEBaudRate = dBaudRateComboBox.Text;

            if (dAddressTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error( "请输入设备地址");
                return;
            }
            config.DEAddress = dAddressTextBox.Text;

            if (!dbHostTextBox.Text.IsPrivateIP())
            {
                Alert.Error("主机地址错误");
                return;
            }
            config.DBHost = dbHostTextBox.Text;

            if (!Tool.IsPrivateNumber(dbPortTextBox.Text))
            {
                Alert.Error("端口地址错误");
                return;
            }
            config.DBPort = dbPortTextBox.Text;

            if (dbDatebaseTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error("请输入数据库名");
                return;
            }
            config.DBDatabase = dbDatebaseTextBox.Text;

            if (dbUserTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error("请输入用户名");
                return;
            }
            config.DBUser = dbUserTextBox.Text;

            if (dbPwdTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error( "请输入密码");
                return;
            }
            config.DBPwd = dbPwdTextBox.Text;

            if (dbPTableTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error("请输入任务表名");
                return;
            }
            config.DBPTable = dbPTableTextBox.Text;

            if (dbPointTableTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error("请输入点位表名");
                return;
            }
            config.DBPointTable = dbPointTableTextBox.Text;

            if (apiAddressTextBox.Text.IsNullOrEmpty())
            {
                Alert.Error("请输入上送接口地址");
                return;
            }
            config.APIAddress = apiAddressTextBox.Text;

            config.CompleteSetup = true;
            this.Close();
        }
    }
}
